var post_url = "http://www.mermexico.com/dpm/projects/hangman/hangman.php";
var preferences_url = "http://www.mermexico.com/dpm/projects/hangman/preferences.php";

function or () {
 for (var i=0; i<or.arguments.length; i++) if (typeof(or.arguments[i]) != "undefined") return or.arguments[i];
 return null;
}

function createDiv( /* [_id], [_firstChild] */ ) {
 var temp_div = document.createElement("DIV");
 if (typeof(arguments[0]) == 'string') temp_div.id = arguments[0];
 if (typeof(arguments[1]) == 'object') temp_div.appendChild(arguments[1]);
 return temp_div;
}

var Notify = {};

Notify.init = function () {
 this.messages = [];
 this.log = [];
 this.notediv = document.createElement("DIV");
 this.notediv.id = "notify_div";
 document.getElementById("body").appendChild(this.notediv);
 window.setInterval("Notify.update();", 500);
};

Notify.addMessage = function ( _message /* , [ _maxage ] */ ) {
 var maxage = 6000;
 if (typeof(arguments[1]) == "number") maxage = arguments[1];
 this.messages[this.messages.length] = { "message" : _message, "timestamp" : Date.now(), "maxage" : maxage };
 this.render();
};

Notify.render = function () {
 if (this.messages.length) {
  this.notediv.style.top = or(window.scrollY, window.pageYOffset, document.body.scrollTop) + 10;
  this.notediv.style.left = or(window.innerWidth, document.body.offsetWidth) - 250;
  this.notediv.innerHTML = "";
  for (var i in this.messages) this.notediv.innerHTML += this.messages[i].message + '<br/>';
  this.notediv.style.display = "block";
 } else {
  this.notediv.innerHTML = "";
  this.notediv.style.display = "none";
 }
};

Notify.update = function () {
  Notify.render();
  var temp_messages = [];
  for (var i in this.messages) {
   var age = Date.now() - this.messages[i].timestamp;
   if (this.messages[i].maxage == 0) temp_messages[temp_messages.length] = this.messages[i];
   else if (this.messages[i].maxage > age) temp_messages[temp_messages.length] = this.messages[i];
  }
  this.messages = temp_messages;
};

var XHR = {};

XHR.getXHRObject = function () {
 if (window.XMLHttpRequest) {
  return new XMLHttpRequest;
 } else if (window.ActiveXObject) {
  return new ActiveXObject("Microsoft.XMLHTTP");
 } else {
  return null;
 }
};

XHR.sendRequest = function ( _cmd, _payload, _callback ) {
 var xreq = XHR.getXHRObject();
 if (!xreq) { return; }
 xreq.open("POST", post_url, true);
 var poststring = "";
 poststring += "cmd=ajaxrequest";
 poststring += "&ajaxcmd=" + escape(_cmd);
 for (var i in _payload) poststring += "&" + escape(i) + "=" + escape(_payload[i]);
 xreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
 xreq.onreadystatechange = function () {
  if (xreq.readyState == 4) {
   if (xreq.status == 200) _callback( eval("(" + xreq.responseText + ")") );
   else _callback(null);
  }
 };
 xreq.send(poststring);
};


XHR.getWordList = function ( ) {
 Notify.addMessage("Fetching word list from server...");
 XHR.sendRequest(
  "getWordList",
  {},
  function ( _response ) {
   if (_response && _response.exitMessage) Notify.addMessage(_response.exitMessage);
   if (!_response || _response.exitCode) return; 
   //console.log(_response.wordlist);
   FlashCard.setWordList(_response.wordlist); 
  }
 );
};

var FlashCard = {};

FlashCard.init = function () {
 this.wordlist = [];
 XHR.getWordList();
 FlashCard.render();
};

FlashCard.setWordList = function (_wordlist) {

 this.wordlist = _wordlist;

};

FlashCard.render = function () {

 index = Math.floor(Math.random()*FlashCard.wordlist.length);
 console.log(index);
 console.log(FlashCard.wordlist[index]);
 if (FlashCard.wordlist[index] != undefined) {
  document.getElementById('flash_lang1').innerHTML = FlashCard.wordlist[index][0];
  document.getElementById('flash_lang2').innerHTML = FlashCard.wordlist[index][1];
 }
 window.setTimeout(FlashCard.render, 5000);
 

};

window.onload = function () {
 Notify.init();
 FlashCard.init();
 
};

